package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.TraversingMapper;
import com.code.dao.PersonMapper;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.Traversing;
import com.code.pojo.Person;
import com.code.service.PersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonMapper personMapper;
    @Autowired
    TraversingMapper traversingMapper;


    @Override
    public PageResult<Person> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Person> personList = personMapper.findLike(queryPageBean);
        PageInfo<Person> pageInfo = new PageInfo<>(personList);
        PageResult<Person> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Person> findPage(Person Person) {
//     PageHelper.startPage(Person.getPage(),Person.getSize());
//        List<Person> PersonList = PersonMapper.findLike(Person);
//        PageInfo<Person> pageInfo = new PageInfo<>(PersonList);
//        PageResult<Person> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Person findById(Integer id) {
        return personMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Person person) {
        personMapper.insert(person);
    }

    @Override
    public void update(Person person) {
        personMapper.updateByPrimaryKeySelective(person);
    }

    @Override
    public void delete(Integer id) {
        personMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Traversing> findTraversing() {
        return traversingMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return personMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return PersonMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Person> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Person> personList = personMapper.findPageNo(queryPageBean);
        PageInfo<Person> pageInfo = new PageInfo<>(personList);
        PageResult<Person> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Person person = personMapper.selectByPrimaryKey(id);
        person.setState(1);
        personMapper.updateByPrimaryKeySelective(person);
    }
}
